
<html>
<head>
    <title>NeHe's OpenGL Lesson 10 for WebGl (Loading And Moving Through A 3D World)</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style type="text/css">
        canvas {
            border: 2px solid black;
            -moz-box-shadow: black 2px 2px 2px;
            background-color: black;
        }
    </style>
    <script src="http://derekliu.blog.51cto.com/attachment/201205/4479510_1337314020.jpg" type="text/javascript"></script>
    <script src="http://derekliu.blog.51cto.com/attachment/201205/4479510_1337314011.jpg" type="text/javascript"></script>

    <!-- Fragment shader program -->

    <script id="shader-fs" type="x-shader/x-fragment">
        varying highp vec2 vTextureCoord;

        uniform sampler2D uSampler;

        void main(void) {
        gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
        }
    </script>

    <!-- Vertex shader program -->

    <script id="shader-vs" type="x-shader/x-vertex">
        attribute highp vec3 aVertexPosition;
        attribute highp vec2 aTextureCoord;

        uniform highp mat4 uNormalMatrix;
        uniform highp mat4 uMVMatrix;
        uniform highp mat4 uPMatrix;

        varying highp vec2 vTextureCoord;

        void main(void) {
        gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
        vTextureCoord = aTextureCoord;
        }
    </script>
</head>

<body>
<canvas id="glcanvas" width="640" height="480">
    Your browser doesn't appear to support the HTML5 <code>&lt;canvas&gt;</code> element.
</canvas>
<script type="text/javascript">
var canvas;
var gl;

var cubeVerticesBuffer;
var cubeVerticesTextureCoordBuffer;
var cubeVerticesIndexBuffer;
var cubeVerticesNormalBuffer;
var cubeRotation = 0.0;
var lastCubeUpdateTime = 0;

var cubeImage;
var cubeTexture;

var mvMatrix;
var shaderProgram;
var vertexPositionAttribute;
var vertexNormalAttribute;
var textureCoordAttribute;
var perspectiveMatrix;

var heading=0.0,xpos=0.0,zpos=0.0,yrot=0.0,walkbias = 0,walkbiasangle = 0,lookupdown = 0.0,z=0.0;
var piover180 = 0.0174532925;

start();
//
// start
//
// Called when the canvas is created to get the ball rolling.
//
function start() {
    canvas = document.getElementById("glcanvas");

    initWebGL();      // Initialize the GL context

    // Only continue if WebGL is available and working

    if (gl) {
        gl.clearColor(0.0, 0.0, 0.0, 1.0);  // Clear to black, fully opaque
        gl.clearDepth(1.0);                 // Clear everything
        gl.enable(gl.DEPTH_TEST);           // Enable depth testing
        gl.depthFunc(gl.LEQUAL);            // Near things obscure far things

        // Initialize the shaders; this is where all the lighting for the
        // vertices and so forth is established.

        initShaders();

        // Here's where we call the routine that builds all the objects
        // we'll be drawing.

        initBuffers();

        // Next, load and set up the textures we'll be using.

        initTextures();

        // Set up to draw the scene periodically.

        setInterval(drawScene, 15);

        document.addEventListener( 'keydown', onDocumentKeyDown, false );
    }
}

//
// initWebGL
//
// Initialize WebGL, returning the GL context or null if
// WebGL isn't available or could not be initialized.
//
function initWebGL() {
    gl = null;

    try {
        gl = canvas.getContext("experimental-webgl");
    }
    catch(e) {
    }

    // If we don't have a GL context, give up now

    if (!gl) {
        alert("Unable to initialize WebGL. Your browser may not support it.");
    }
}

//
// initBuffers
//
// Initialize the buffers we'll need. For this demo, we just have
// one object -- a simple two-dimensional cube.
//
function initBuffers() {

    // Create a buffer for the cube's vertices.

    cubeVerticesBuffer = gl.createBuffer();

    // Select the cubeVerticesBuffer as the one to apply vertex
    // operations to from here out.

    gl.bindBuffer(gl.ARRAY_BUFFER, cubeVerticesBuffer);

    // Now create an array of vertices for the cube.

    var vertices = [
        // Floor 1
        -3.0,  0.0, -3.0,
        -3.0,  0.0,  3.0,
        3.0,  0.0,  3.0,
        -3.0,  0.0, -3.0,
        3.0,  0.0, -3.0,
        3.0,  0.0,  3.0,
        // Ceiling 1
        -3.0,  1.0, -3.0,
        -3.0,  1.0,  3.0,
        3.0,  1.0,  3.0,
        -3.0,  1.0, -3.0,
        3.0,  1.0, -3.0,
        3.0,  1.0,  3.0,
        // A1
        -2.0,  1.0,  -2.0,
        -2.0,  0.0,  -2.0,
        -0.5,  0.0,  -2.0,
        -2.0,  1.0,  -2.0,
        -0.5,  1.0,  -2.0,
        -0.5,  0.0,  -2.0,
        // A2
        2.0,  1.0,  -2.0,
        2.0,  0.0,  -2.0,
        0.5,  0.0,  -2.0,
        2.0,  1.0,  -2.0,
        0.5,  1.0,  -2.0,
        0.5,  0.0,  -2.0,
        // B1
        -2.0,  1.0,   2.0,
        -2.0,  0.0,   2.0,
        -0.5,  0.0,   2.0,
        -2.0,  1.0,   2.0,
        -0.5,  1.0,   2.0,
        -0.5,  0.0,   2.0,
        // B2
        2.0,  1.0,   2.0,
        2.0,  0.0,   2.0,
        0.5,  0.0,   2.0,
        2.0,  1.0,   2.0,
        0.5,  1.0,   2.0,
        0.5,  0.0,   2.0,
        // C1
        -2.0,  1.0,  -2.0,
        -2.0,  0.0,  -2.0,
        -2.0,  0.0,  -0.5,
        -2.0,  1.0,  -2.0,
        -2.0,  1.0,  -0.5,
        -2.0,  0.0,  -0.5,
        // C2
        -2.0,  1.0,   2.0,
        -2.0,  0.0,   2.0,
        -2.0,  0.0,   0.5,
        -2.0,  1.0,   2.0,
        -2.0,  1.0,   0.5,
        -2.0,  0.0,   0.5,
        // D1
        2.0,  1.0,  -2.0,
        2.0,  0.0,  -2.0,
        2.0,  0.0,  -0.5,
        2.0,  1.0,  -2.0,
        2.0,  1.0,  -0.5,
        2.0,  0.0,  -0.5,
        // D2
        2.0,  1.0,   2.0,
        2.0,  0.0,   2.0,
        2.0,  0.0,   0.5,
        2.0,  1.0,   2.0,
        2.0,  1.0,   0.5,
        2.0,  0.0,   0.5,
        // Upper hallway - L
        -0.5,  1.0,  -3.0,
        -0.5,  0.0,  -3.0,
        -0.5,  0.0,  -2.0,
        -0.5,  1.0,  -3.0,
        -0.5,  1.0,  -2.0,
        -0.5,  0.0,  -2.0,
        // Upper hallway - R
        0.5,  1.0,  -3.0,
        0.5,  0.0,  -3.0,
        0.5,  0.0,  -2.0,
        0.5,  1.0,  -3.0,
        0.5,  1.0,  -2.0,
        0.5,  0.0,  -2.0,
        // Lower hallway - L
        -0.5,  1.0,  3.0,
        -0.5,  0.0,  3.0,
        -0.5,  0.0,  2.0,
        -0.5,  1.0,  3.0,
        -0.5,  1.0,  2.0,
        -0.5,  0.0,  2.0,
        // Lower hallway - R
        0.5,  1.0,  3.0,
        0.5,  0.0,  3.0,
        0.5,  0.0,  2.0,
        0.5,  1.0,  3.0,
        0.5,  1.0,  2.0,
        0.5,  0.0,  2.0,
        // Left hallway - Lw
        -3.0,  1.0,  0.5,
        -3.0,  0.0,  0.5,
        -2.0,  0.0,  0.5,
        -3.0,  1.0,  0.5,
        -2.0,  1.0,  0.5,
        -2.0,  0.0,  0.5,
        // Left hallway - Hi
        -3.0,  1.0,  -0.5,
        -3.0,  0.0,  -0.5,
        -2.0,  0.0,  -0.5,
        -3.0,  1.0,  -0.5,
        -2.0,  1.0,  -0.5,
        -2.0,  0.0,  -0.5,
        // Right hallway - Lw
        3.0,  1.0,  0.5,
        3.0,  0.0,  0.5,
        2.0,  0.0,  0.5,
        3.0,  1.0,  0.5,
        2.0,  1.0,  0.5,
        2.0,  0.0,  0.5,
        // Right hallway - Hi
        3.0,  1.0,  -0.5,
        3.0,  0.0,  -0.5,
        2.0,  0.0,  -0.5,
        3.0,  1.0,  -0.5,
        2.0,  1.0,  -0.5,
        2.0,  0.0,  -0.5
    ];

    // Now pass the list of vertices into WebGL to build the shape. We
    // do this by creating a Float32Array from the JavaScript array,
    // then use it to fill the current vertex buffer.

    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);

    // Map the texture onto the cube's faces.

    cubeVerticesTextureCoordBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, cubeVerticesTextureCoordBuffer);

    var textureCoordinates = [
        // Floor 1
        0.0, 6.0,
        0.0, 0.0,
        6.0, 0.0,
        0.0, 6.0,
        6.0, 6.0,
        6.0, 0.0,
        // Ceiling 1
        0.0, 6.0,
        0.0, 0.0,
        6.0, 0.0,
        0.0, 6.0,
        6.0, 6.0,
        6.0, 0.0,
        // A1
        0.0, 1.0,
        0.0, 0.0,
        1.5, 0.0,
        0.0, 1.0,
        1.5, 1.0,
        1.5, 0.0,
        // A2
        2.0, 1.0,
        2.0, 0.0,
        0.5, 0.0,
        2.0, 1.0,
        0.5, 1.0,
        0.5, 0.0,
        // B1
        2.0, 1.0,
        2.0, 0.0,
        0.5, 0.0,
        2.0, 1.0,
        0.5, 1.0,
        0.5, 0.0,
        // B2
        2.0, 1.0,
        2.0, 0.0,
        0.5, 0.0,
        2.0, 1.0,
        0.5, 1.0,
        0.5, 0.0,
        // C1
        0.0, 1.0,
        0.0, 0.0,
        1.5, 0.0,
        0.0, 1.0,
        1.5, 1.0,
        1.5, 0.0,
        // C2
        2.0, 1.0,
        2.0, 0.0,
        0.5, 0.0,
        2.0, 1.0,
        0.5, 1.0,
        0.5, 0.0,
        // D1
        0.0, 1.0,
        0.0, 0.0,
        1.5, 0.0,
        0.0, 1.0,
        1.5, 1.0,
        1.5, 0.0,
        // D2
        2.0, 1.0,
        2.0, 0.0,
        0.5, 0.0,
        2.0, 1.0,
        0.5, 1.0,
        0.5, 0.0,
        // Upper hallway - L
        0.0, 1.0,
        0.0, 0.0,
        1.0, 0.0,
        0.0, 1.0,
        1.0, 1.0,
        1.0, 0.0,
        // Upper hallway - R
        0.0, 1.0,
        0.0, 0.0,
        1.0, 0.0,
        0.0, 1.0,
        1.0, 1.0,
        1.0, 0.0,
        // Lower hallway - L
        0.0, 1.0,
        0.0, 0.0,
        1.0, 0.0,
        0.0, 1.0,
        1.0, 1.0,
        1.0, 0.0,
        // Lower hallway - R
        0.0, 1.0,
        0.0, 0.0,
        1.0, 0.0,
        0.0, 1.0,
        1.0, 1.0,
        1.0, 0.0,
        // Left hallway - Lw
        1.0, 1.0,
        1.0, 0.0,
        0.0, 0.0,
        1.0, 1.0,
        0.0, 1.0,
        0.0, 0.0,
        // Left hallway - Hi
        1.0, 1.0,
        1.0, 0.0,
        0.0, 0.0,
        1.0, 1.0,
        0.0, 1.0,
        0.0, 0.0,
        // Right hallway - Lw
        1.0, 1.0,
        1.0, 0.0,
        0.0, 0.0,
        1.0, 1.0,
        0.0, 1.0,
        0.0, 0.0,
        // Right hallway - Hi
        1.0, 1.0,
        1.0, 0.0,
        0.0, 0.0,
        1.0, 1.0,
        0.0, 1.0,
        0.0, 0.0
    ];

    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoordinates),
            gl.STATIC_DRAW);

    // Build the element array buffer; this specifies the indices
    // into the vertex array for each face's vertices.

    cubeVerticesIndexBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVerticesIndexBuffer);

    // This array defines each face as two triangles, using the
    // indices into the vertex array to specify each triangle's
    // position.

    var cubeVertexIndices = [
        0,  1,  2,  3,  4,  5,
        6,  7,  8,  9,  10, 11,
        12, 13, 14, 15, 16, 17,
        18, 19, 20, 21, 22, 23,
        24, 25, 26, 27, 28, 29,
        30, 31, 32, 33, 34, 35,
        36, 37, 38, 39, 40, 41,
        42, 43, 44, 45, 46, 47,
        48, 49, 50, 51, 52, 53,
        54, 55, 56, 57, 58, 59,
        60, 61, 62, 63, 64, 65,
        66, 67, 68, 69, 70, 71,
        72, 73, 74, 75, 76, 77,
        78, 79, 80, 81, 82, 83,
        84, 85, 86, 87, 88, 89,
        90, 91, 92, 93, 94, 95,
        96, 97, 98, 99, 100,101,
        102,103,104,105,106,107
    ]

    // Now send the element array to GL

    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,
            new Uint16Array(cubeVertexIndices), gl.STATIC_DRAW);
}

//
// initTextures
//
// Initialize the textures we'll be using, then initiate a load of
// the texture images. The handleTextureLoaded() callback will finish
// the job; it gets called each time a texture finishes loading.
//
function initTextures() {
    cubeTexture = gl.createTexture();
    cubeImage = new Image();
    cubeImage.crossOrigin = "";
    cubeImage.onload = function() { handleTextureLoaded(cubeImage, cubeTexture); }
    cubeImage.src = "http://derekliu.blog.51cto.com/album/4479510/133601724534.png";
}

function handleTextureLoaded(image, texture) {
    gl.bindTexture(gl.TEXTURE_2D, texture);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
    gl.generateMipmap(gl.TEXTURE_2D);
    gl.bindTexture(gl.TEXTURE_2D, null);
}

//
// drawScene
//
// Draw the scene.
//
function drawScene() {
    // Clear the canvas before we start drawing on it.

    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

    // Establish the perspective with which we want to view the
    // scene. Our field of view is 45 degrees, with a width/height
    // ratio of 640:480, and we only want to see objects between 0.1 units
    // and 100 units away from the camera.

    perspectiveMatrix = makePerspective(45, 640.0/480.0, 0.1, 100.0);

    loadIdentity();

    var xtrans = -xpos;
    var ztrans = -zpos;
    var ytrans = -walkbias-0.25;
    var sceneroty = 360.0 - yrot;

    mvRotate(lookupdown,[1.0,0,0]);
    mvRotate(sceneroty,[0,1.0,0]);
    mvTranslate([xtrans, ytrans, ztrans]);

    // Draw the cube by binding the array buffer to the cube's vertices
    // array, setting attributes, and pushing it to GL.

    gl.bindBuffer(gl.ARRAY_BUFFER, cubeVerticesBuffer);
    gl.vertexAttribPointer(vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0);

    // Set the texture coordinates attribute for the vertices.

    gl.bindBuffer(gl.ARRAY_BUFFER, cubeVerticesTextureCoordBuffer);
    gl.vertexAttribPointer(textureCoordAttribute, 2, gl.FLOAT, false, 0, 0);

    // Specify the texture to map onto the faces.

    gl.activeTexture(gl.TEXTURE0);
    gl.bindTexture(gl.TEXTURE_2D, cubeTexture);
    gl.uniform1i(gl.getUniformLocation(shaderProgram, "uSampler"), 0);

    // Draw.
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVerticesIndexBuffer);
    setMatrixUniforms();
    gl.drawElements(gl.TRIANGLES, 108, gl.UNSIGNED_SHORT, 0);

    // Update the rotation for the next draw, if it's time to do so.

    var currentTime = (new Date).getTime();
    lastCubeUpdateTime = currentTime;
}

function onDocumentKeyDown ( event ) {
    //event.preventDefault();
    if( event.keyCode == 84 /*T*/){
    }
    if (event.keyCode == 33 /*pgup*/) {
        z -= 0.02;
    }

    if (event.keyCode == 34 /*pgdn*/) {
        z += 0.02;
    }

    if (event.keyCode == 38 /*up*/) {

        xpos -= Math.sin(heading * piover180) * 0.05;
        zpos -= Math.cos(heading * piover180) * 0.05;
        if (walkbiasangle >= 359.0) {
            walkbiasangle = 0.0;
        }
    else {
            walkbiasangle += 10;
        }
        walkbias = Math.sin(walkbiasangle * piover180) / 20.0;
    }

    if (event.keyCode == 40 /*down*/) {
        xpos += Math.sin(heading * piover180) * 0.05;
        zpos += Math.cos(heading * piover180) * 0.05;
        if (walkbiasangle <= 1.0) {
            walkbiasangle = 359.0;
        }
    else {
            walkbiasangle -= 10;
        }
        walkbias = Math.sin(walkbiasangle * piover180) / 20.0;
    }

    if (event.keyCode == 39 /*right*/) {
        heading -= 1.0;
        yrot = heading;
    }

    if (event.keyCode == 37 /*left*/) {
        heading += 1.0;
        yrot = heading;
    }

    if (event.keyCode == 33 /*pgup*/) {
        lookupdown -= 1.0;
    }

    if (event.keyCode == 34 /*pgdn*/) {
        lookupdown += 1.0;
    }
}

//
// initShaders
//
// Initialize the shaders, so WebGL knows how to light our scene.
//
function initShaders() {
    var fragmentShader = getShader(gl, "shader-fs");
    var vertexShader = getShader(gl, "shader-vs");

    // Create the shader program

    shaderProgram = gl.createProgram();
    gl.attachShader(shaderProgram, vertexShader);
    gl.attachShader(shaderProgram, fragmentShader);
    gl.linkProgram(shaderProgram);

    // If creating the shader program failed, alert

    if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
        alert("Unable to initialize the shader program.");
    }

    gl.useProgram(shaderProgram);

    vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
    gl.enableVertexAttribArray(vertexPositionAttribute);

    textureCoordAttribute = gl.getAttribLocation(shaderProgram, "aTextureCoord");
    gl.enableVertexAttribArray(textureCoordAttribute);
}

//
// getShader
//
// Loads a shader program by scouring the current document,
// looking for a script with the specified ID.
//
function getShader(gl, id) {
    var shaderScript = document.getElementById(id);

    // Didn't find an element with the specified ID; abort.

    if (!shaderScript) {
        return null;
    }

    // Walk through the source element's children, building the
    // shader source string.

    var theSource = "";
    var currentChild = shaderScript.firstChild;

    while(currentChild) {
        if (currentChild.nodeType == 3) {
            theSource += currentChild.textContent;
        }

        currentChild = currentChild.nextSibling;
    }

    // Now figure out what type of shader script we have,
    // based on its MIME type.

    var shader;

    if (shaderScript.type == "x-shader/x-fragment") {
        shader = gl.createShader(gl.FRAGMENT_SHADER);
    } else if (shaderScript.type == "x-shader/x-vertex") {
        shader = gl.createShader(gl.VERTEX_SHADER);
    } else {
        return null;  // Unknown shader type
    }

    // Send the source to the shader object

    gl.shaderSource(shader, theSource);

    // Compile the shader program

    gl.compileShader(shader);

    // See if it compiled successfully

    if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
        alert("An error occurred compiling the shaders: " + gl.getShaderInfoLog(shader));
        return null;
    }

    return shader;
}

//
// Matrix utility functions
//

function loadIdentity() {
    mvMatrix = Matrix.I(4);
}

function multMatrix(m) {
    mvMatrix = mvMatrix.x(m);
}

function mvTranslate(v) {
    multMatrix(Matrix.Translation($V([v[0], v[1], v[2]])).ensure4x4());
}

function setMatrixUniforms() {
    var pUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
    gl.uniformMatrix4fv(pUniform, false, new Float32Array(perspectiveMatrix.flatten()));

    var mvUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
    gl.uniformMatrix4fv(mvUniform, false, new Float32Array(mvMatrix.flatten()));

    var normalMatrix = mvMatrix.inverse();
    normalMatrix = normalMatrix.transpose();
    var nUniform = gl.getUniformLocation(shaderProgram, "uNormalMatrix");
    gl.uniformMatrix4fv(nUniform, false, new Float32Array(normalMatrix.flatten()));
}

var mvMatrixStack = [];

function mvPushMatrix(m) {
    if (m) {
        mvMatrixStack.push(m.dup());
        mvMatrix = m.dup();
    } else {
        mvMatrixStack.push(mvMatrix.dup());
    }
}

function mvPopMatrix() {
    if (!mvMatrixStack.length) {
        throw("Can't pop from an empty matrix stack.");
    }

    mvMatrix = mvMatrixStack.pop();
    return mvMatrix;
}

function mvRotate(angle, v) {
    var inRadians = angle * Math.PI / 180.0;

    var m = Matrix.Rotation(inRadians, $V([v[0], v[1], v[2]])).ensure4x4();
    multMatrix(m);
}
</script>
</body>
</html>